package org.linlinjava.litemall.core.util;

import com.alibaba.fastjson.JSONObject;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.BizLicenseOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.BizLicenseOCRResponse;
import org.apache.commons.codec.binary.Base64;

import java.io.*;


public class BizLicenseOCR {

    public static String getBase64(File file) throws IOException {
        byte[] bytes = loadFile(file);
        byte[] encoded = Base64.encodeBase64(bytes);
        String encodedString = new String(encoded);
        return encodedString;
    }
    private static byte[] loadFile(File file) throws IOException {
        InputStream is = new FileInputStream(file);

        long length = file.length();
        if (length > Integer.MAX_VALUE) {
            // File is too large
        }
        byte[] bytes = new byte[(int)length];

        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
                && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }

        if (offset < bytes.length) {
            throw new IOException("Could not completely read file "+file.getName());
        }

        is.close();
        return bytes;
    }

    public static String licenseOCR(String url) throws TencentCloudSDKException {
        Credential cred = new Credential("AKIDXqy5jACmW3LNGuvQVlnbVIBqFQN4Hs1u", "pRpzvbKq66rOgiUM2Z6TeeSRWO0NUO5Y");

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("ocr.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);

        OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);

        String params = "{\"ImageUrl\":\"" + url + "\"}";
        BizLicenseOCRRequest req = BizLicenseOCRRequest.fromJsonString(params, BizLicenseOCRRequest.class);

        BizLicenseOCRResponse resp = client.BizLicenseOCR(req);
        return JSONObject.toJSONString(resp);
    }

    public static String licenseOCR(File file) throws TencentCloudSDKException, IOException {
        Credential cred = new Credential("AKIDXqy5jACmW3LNGuvQVlnbVIBqFQN4Hs1u", "pRpzvbKq66rOgiUM2Z6TeeSRWO0NUO5Y");

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("ocr.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);

        OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);



        String params = "{\"ImageBase64\":\"" + getBase64(file) + "\"}";
        BizLicenseOCRRequest req = BizLicenseOCRRequest.fromJsonString(params, BizLicenseOCRRequest.class);

        BizLicenseOCRResponse resp = client.BizLicenseOCR(req);
        return JSONObject.toJSONString(resp);
    }

    public static JSONObject getLicenseInfo(String url) throws TencentCloudSDKException {
        return JSONObject.parseObject(licenseOCR(url));
    }

    public static JSONObject getLicenseInfo(File file) throws TencentCloudSDKException, IOException {
        return JSONObject.parseObject(licenseOCR(file));
    }

    public static void main(String [] args) {
        try{
            String base64 = licenseOCR(new File("C:\\Users\\Riko\\Desktop\\2018030311021606.jpg"));
            System.out.println(base64);
        } catch (Exception e) {
            System.out.println(e.toString());
        }

    }

}